----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
-- Description: RFID-Reader (XGCS49LB201) controls KNX-Objects for a certain time
----------------------------------------------------------------------------------------------------
-- Script needs to be placed within \Scripting\Resident 
-- You need to update the dedicated group address
-- and the staircasetime in seconds. In case of minutes or
-- hours you need to calculate accordingly.
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
-- NEEDS TO BE ADAPTED! ----------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

RFID_Status_Obj_GA = '36/1/1'
LED_Obj_GA = '36/1/11'
UID1_Obj_GA = '36/1/3'
UID2_Obj_GA = '36/1/4'

----------------------------------------------------------------------------------------------------
-- Staircase time in seconds -----------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

-- if you need minutes please multiply with 60
-- staircasetime = 2 * 60 (example)

--staircasetime = 2 * 60
staircasetime = 30


----------------------------------------------------------------------------------------------------
-- Alerts + Logs ----------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

-- In case you want to document the detected Tags you can activate the following options by exhange
-- "false" against "true".
-- Alerts can be reviewed by the use of the dedicated App or within the tab "Alerts"

-- 1. "Log" each known detected Tag (Tag Number, 
Log_known_Tags = false

-- 2. "Log" each unknown detected Tag
Log_unknown_Tags = false

-- 3. "Alert" by each known detected Tag. 
Alert_by_known_Tags = false

-- 4. "Alert" by each unknown detected Tag
Alert_by_unknown_Tags = false


----------------------------------------------------------------------------------------------------
-- RFID-Tag-Declaration ----------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

RFID_Tag_UID = {}
		RFID_Tag_UID[1], RFID_Tag_UID[2] = 0, 0							-- RFID-Tag 1
		RFID_Tag_UID[3], RFID_Tag_UID[4] = 0, 0						-- RFID-Tag 2
		RFID_Tag_UID[5], RFID_Tag_UID[6] = 0, 0						-- RFID-Tag 3
		RFID_Tag_UID[7], RFID_Tag_UID[8] = 0, 0						-- RFID-Tag 4
		RFID_Tag_UID[9], RFID_Tag_UID[10] = 0, 0					-- RFID-Tag 5
		RFID_Tag_UID[11], RFID_Tag_UID[12] = 0, 0					-- RFID-Tag 6
		RFID_Tag_UID[13], RFID_Tag_UID[14] = 0, 0					-- RFID-Tag 7
		RFID_Tag_UID[15], RFID_Tag_UID[16] = 0, 0					-- RFID-Tag 8
		RFID_Tag_UID[17], RFID_Tag_UID[18] = 0, 0					-- RFID-Tag 9
		RFID_Tag_UID[19], RFID_Tag_UID[20] = 0, 0					-- RFID-Tag 10
		RFID_Tag_UID[21], RFID_Tag_UID[22] = 0, 0					-- RFID-Tag 11
		RFID_Tag_UID[23], RFID_Tag_UID[24] = 0, 0					-- RFID-Tag 12
		RFID_Tag_UID[25], RFID_Tag_UID[26] = 0, 0				-- RFID-Tag 13
		RFID_Tag_UID[27], RFID_Tag_UID[28] = 0, 0				-- RFID-Tag 14
		RFID_Tag_UID[29], RFID_Tag_UID[30] = 0, 0				-- RFID-Tag 15
		RFID_Tag_UID[31], RFID_Tag_UID[32] = 0, 0				-- RFID-Tag 16
		RFID_Tag_UID[33], RFID_Tag_UID[34] = 0, 0						-- RFID-Tag 17
		RFID_Tag_UID[35], RFID_Tag_UID[36] = 0, 0					-- RFID-Tag 18
		RFID_Tag_UID[37], RFID_Tag_UID[38] = 0, 0					-- RFID-Tag 19
		RFID_Tag_UID[39], RFID_Tag_UID[40] = 0, 0					-- RFID-Tag 20
		RFID_Tag_UID[41], RFID_Tag_UID[42] = 0, 0					-- RFID-Tag 21
		RFID_Tag_UID[43], RFID_Tag_UID[44] = 0, 0					-- RFID-Tag 22
		RFID_Tag_UID[45], RFID_Tag_UID[46] = 0, 0					-- RFID-Tag 23
		RFID_Tag_UID[47], RFID_Tag_UID[48] = 0, 0					-- RFID-Tag 24
		RFID_Tag_UID[49], RFID_Tag_UID[50] = 0, 0					-- RFID-Tag 25
		RFID_Tag_UID[51], RFID_Tag_UID[52] = 0, 0					-- RFID-Tag 26
		RFID_Tag_UID[53], RFID_Tag_UID[54] = 0, 0					-- RFID-Tag 27
		RFID_Tag_UID[55], RFID_Tag_UID[56] = 0, 0					-- RFID-Tag 28
		RFID_Tag_UID[57], RFID_Tag_UID[58] = 0, 0				-- RFID-Tag 29
		RFID_Tag_UID[59], RFID_Tag_UID[60] = 0, 0				-- RFID-Tag 30


----------------------------------------------------------------------------------------------------
-- Group-Address-Declaration -----------------------------------------------------------------------
-- This GA will be used (staircase timer) when the dedicated RFID-Tag is detected ------------------
----------------------------------------------------------------------------------------------------

GA_Declaration = {}
		GA_Declaration[1] = '36/2/1'							-- RFID-Tag 1
		GA_Declaration[2] = '36/2/2'							-- RFID-Tag 2
		GA_Declaration[3] = '36/2/3'							-- RFID-Tag 3
		GA_Declaration[4] = '36/2/4'							-- RFID-Tag 4
		GA_Declaration[5] = '36/2/5'							-- RFID-Tag 5
		GA_Declaration[6] = '36/2/6'							-- RFID-Tag 6
		GA_Declaration[7] = '36/2/7'							-- RFID-Tag 7
		GA_Declaration[8] = '36/2/8'							-- RFID-Tag 8
		GA_Declaration[9] = '36/2/9'							-- RFID-Tag 9
		GA_Declaration[10] = '36/2/10'							-- RFID-Tag 10
		GA_Declaration[11] = '36/2/11'							-- RFID-Tag 11
		GA_Declaration[12] = '36/2/12'							-- RFID-Tag 12
		GA_Declaration[13] = '36/2/13'							-- RFID-Tag 13
		GA_Declaration[14] = '36/2/14'							-- RFID-Tag 14
		GA_Declaration[15] = '36/2/15'							-- RFID-Tag 15
		GA_Declaration[16] = '36/2/16'							-- RFID-Tag 16
		GA_Declaration[17] = '36/2/17'							-- RFID-Tag 17
		GA_Declaration[18] = '36/2/18'							-- RFID-Tag 18
		GA_Declaration[19] = '36/2/19'							-- RFID-Tag 19
		GA_Declaration[20] = '36/2/20'							-- RFID-Tag 20
		GA_Declaration[21] = '36/2/21'							-- RFID-Tag 21
		GA_Declaration[22] = '36/2/22'							-- RFID-Tag 22
		GA_Declaration[23] = '36/2/23'							-- RFID-Tag 23
		GA_Declaration[24] = '36/2/24'							-- RFID-Tag 24
		GA_Declaration[25] = '36/2/25'							-- RFID-Tag 25
		GA_Declaration[26] = '36/2/26'							-- RFID-Tag 26
		GA_Declaration[27] = '36/2/27'							-- RFID-Tag 27
		GA_Declaration[28] = '36/2/28'							-- RFID-Tag 28
		GA_Declaration[29] = '36/2/29'							-- RFID-Tag 29
		GA_Declaration[30] = '36/2/30'							-- RFID-Tag 30


-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
----------------------------------------------------------------------------------------------------
-- DONT CHANGE CODE BELOW --------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


----------------------------------------------------------------------------------------------------
-- LED Colour Definition ---------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

LED_GREEN = 2
LED_RED = 4
LED_BLUE = 1
LED_Yellow = 6
LED_OFF = 0


----------------------------------------------------------------------------------------------------
-- Get RFID-Tag Status -----------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
Tag_Present = bit.band(grp.getvalue(RFID_Status_Obj_GA), '0x01')

New_RFID_Tag_Nbr = 0		-- Status NO new RFID-Tag

if Tag_Present > 0 then
  -----------------------
  -- RFID-Tag detected --
  -- Get RFID-Tag-UIDs
  Tag_UID_1 = grp.getvalue(UID1_Obj_GA)
	Tag_UID_2 = grp.getvalue(UID2_Obj_GA)
	
	for i = 1, 59, 2 do
  	if (RFID_Tag_UID[i] == Tag_UID_1) and (RFID_Tag_UID[i+1] == Tag_UID_2) then
    	New_RFID_Tag_Nbr = (i+1)/2 	-- Status (1-30) known RFID-Tag
  	end
	end
	if New_RFID_Tag_Nbr == 0 then
    New_RFID_Tag_Nbr = 255		-- Status unknown RFID-Tag
  end
end

------------------------------------------------------------------------------------------------------
-- Get previous Status -------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------

-- Just for startup ----------------------------------------------------------------------------------
if storage.get('Status_Dealer') == nil then
  storage.set('Status_Dealer', 0)
end 
Status_Dealer = storage.get('Status_Dealer')


------------------------------------------------------------------------------------------------------
-- No change at all ----------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
if Status_Dealer == 0 and New_RFID_Tag_Nbr == 0 then
  if grp.getvalue(LED_Obj_GA) > LED_OFF then
    grp.write(LED_Obj_GA, LED_OFF)	-- Set LED OFF
  end
end


------------------------------------------------------------------------------------------------------
-- Nothing ongoing before but NOW new known Tag detected ---------------------------------------------
------------------------------------------------------------------------------------------------------
if Status_Dealer == 0 and New_RFID_Tag_Nbr > 0 and New_RFID_Tag_Nbr <255 then
  grp.write(LED_Obj_GA, LED_GREEN)	-- Set LED to Green
  storage.set('Status_Dealer', New_RFID_Tag_Nbr)
  grp.write(GA_Declaration[New_RFID_Tag_Nbr], true)
  if Log_known_Tags then
  	log(New_RFID_Tag_Nbr, Tag_UID_1, Tag_UID_2)
  end
  if Alert_by_known_Tags then
  	alert(string.format('TAG Nummer = %d / TAG ID1 = %d / TAG ID2 = %d', tostring(New_RFID_Tag_Nbr), tostring(Tag_UID_1), tostring(Tag_UID_2)))
  end
end


------------------------------------------------------------------------------------------------------
-- One Tag activ and still detected ------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
if Status_Dealer > 0 and New_RFID_Tag_Nbr > 0 and Status_Dealer == New_RFID_Tag_Nbr then
  -- Check if dedicated Colour is already set
  if grp.getvalue(LED_Obj_GA) ~= LED_BLUE then
    grp.write(LED_Obj_GA, LED_BLUE)	-- Set LED to Blue
  end
end


------------------------------------------------------------------------------------------------------
-- One Tag activ and nothing detected ----------------------------------------------------------------
------------------------------------------------------------------------------------------------------
if Status_Dealer > 0 and Status_Dealer <255 and New_RFID_Tag_Nbr == 0 then
  -- Check if dedicated Colour is already set
  if grp.getvalue(LED_Obj_GA) ~= LED_BLUE then
    grp.write(LED_Obj_GA, LED_BLUE)	-- Set LED to Blue
  end
  -- find required object data
	obj = grp.find(GA_Declaration[Status_Dealer])
	--obj = grp.find(string.format('36/2/%d', tostring(Status_Dealer)))
	-- object exists and current state is "on"?
	if obj and obj.data then
  	-- delta is in seconds
  	delta = os.time() - obj.updatetime
    -- switch off when time is expired
  	if delta >= staircasetime then
  		grp.write(GA_Declaration[Status_Dealer], false)
      storage.set('Status_Dealer', 0)
  	end
	end
end


------------------------------------------------------------------------------------------------------
-- One Tag activ and a second one is detected --------------------------------------------------------
------------------------------------------------------------------------------------------------------
if Status_Dealer > 0 and New_RFID_Tag_Nbr > 0 and New_RFID_Tag_Nbr < 255 and New_RFID_Tag_Nbr ~= Status_Dealer then
  -- Check if dedicated Colour is already set
  if grp.getvalue(LED_Obj_GA) ~= LED_Yellow then
    grp.write(LED_Obj_GA, LED_Yellow)	-- Set LED to Blue
  end
end


------------------------------------------------------------------------------------------------------
-- Wrong RFID-Tag detected ---------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
if New_RFID_Tag_Nbr == 255 then
  grp.write(LED_Obj_GA, LED_RED)	-- Set LED to Red
  if Log_unknown_Tags then
    log('unknown', Tag_UID_1, Tag_UID_2)
  end
  if Alert_by_unknown_Tags then
  	alert(string.format('TAG Nummer = unknown / TAG ID1 = %d / TAG ID2 = %d', tostring(Tag_UID_1), tostring(Tag_UID_2)))
  end
end